package com.xy.controller;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xy.constant.Constant;
import com.xy.util.PropertiesUtil;

@Controller
public class UpLoadFileController {
	private Logger log = Logger.getLogger(UpLoadFileController.class);

	@RequestMapping("downLoadFile")
	public ResponseEntity<byte[]> downLoadFileMVC(String filePath, String fileRealName) {
		File file = null;
		HttpHeaders headers = null;
		byte[] body = null;
		try {
			// 解决文件名中文乱码
			fileRealName = new String(fileRealName.getBytes(), "ISO-8859-1");
			file = new File(PropertiesUtil.getValue(Constant.UPLOAD_PATH) + filePath);
			headers = new HttpHeaders();
			// 设置文件类型，文件名称
			headers.setContentDispositionFormData("attachment", fileRealName);
			// 设置响应类型
			headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
			body = FileUtils.readFileToByteArray(file);
		} catch (UnsupportedEncodingException e) {
			log.info("----文件转换字节错误！", e);
			e.printStackTrace();
		} catch (IOException e) {
			log.info("----文件转换字节错误！", e);
		}
		return new ResponseEntity<byte[]>(body, headers, HttpStatus.CREATED);

	}
}
